const yapi = require('../yapi.js');
const baseModel = require('./base.js');
// var mongoose = require('mongoose');
// var Schema = mongoose.Schema;

class dubboMethod extends baseModel {
    getName() {
        return 'dubbo_case';
    }

    getSchema() {
        return {
            casename: { type: String, required: true },  // 测试实例名称
            cname: { type: String, required: true },  // 测试实例中文名
            cuserid: { type: Number, required: true },  // 创建人id
            euserid: Number,  // 修改人id
            col_id: { type: Number, required: true},  // 测试集合id
            index: { type: Number, default: 0},
            projectid: { type: Number, required: true },  // dubbo项目id
            method_id: Number,  // 方法id
            infid: { type: Number, required: true },  // 所属接口id
            ctime: Number,  // 创建时间
            etime: Number,  // 最后修改时间
            case_env: { type: String },  // 测试环境
            inname: String,
            inbody: String,
            outname: String,
            outbody: String,
            status: {
                type: Number,
                required: false,
                enum: [0, 1, 2 ,3]  //0:设计中;1:sit测试;2:uat测试;3:已上线
            },
            isapi: { type: Number, required: true, enum: [0, 1], default: 0 },  //0: 非api;1: api
            url: String,
            mock_verify: { type: Boolean, default: false },
            enable_script: { type: Boolean, default: false },
            test_script: String
        };
    }

    save(data) {
        let m = new this.model(data);
        return m.save();
    }

    //获取全部测试方法信息
    getCaseListCount() {
        return this.model.countDocuments({});
    }

    get(id) {
        return this.model
            .findOne({
                _id: id
            })
            .exec();
    }

    list(col_id, select) {
        select = select || '_id casename cname cuserid euserid col_id index infid projectid inname inbody outname outbody';
        if (select === 'all') {
            return this.model
                .find({
                    col_id: col_id
                })
                .exec();
        }
        return this.model
            .find({
                col_id: col_id
            })
            .select(select)
            .exec();
    }

    del(id) {
        return this.model.remove({
            _id: id
        });
    }

    delByDubboId(id) {
        return this.model.remove({
            projectid: id
        });
    }

    delByInterfaceId(id) {
        return this.model.remove({
            infid: id
        });
    }

    delByCol(id) {
        return this.model.remove({
            col_id: id
        });
    }

    up(id, data) {
        data.etime = yapi.commons.time();
        return this.model.update({ _id: id }, data);
    }

    upCaseIndex(id, index) {
        return this.model.update(
            {
                _id: id
            },
            {
                index: index
            }
        );
    }
}

module.exports = dubboMethod;
